package com.hd.homework0412.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hd.homework0412.entity.Category;
import com.hd.homework0412.service.CategoryService;
import com.hd.homework0412.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    CategoryService categoryService;
    @GetMapping("/getAll")
    public List<Category> getAll(){
        List<Category> categories  = (List<Category>) categoryService.getAll();
        return categories;
    }
    @GetMapping("/getAll1")
    public ResultVo get(){
        ResultVo resultVo = new ResultVo();
        List<Category> categories = categoryService.list();
        for (Category category : categories) {
            QueryWrapper<Category> wrapper = new QueryWrapper<>();
            wrapper.eq("parent_id",category.getCategoryId());
            List<Category> categoryList = categoryService.list(wrapper);
            category.setCategories(categoryList);
            for (Category category1 : categoryList) {
                QueryWrapper<Category> wrapper1 = new QueryWrapper<>();
                wrapper1.eq("parent_id",category1.getCategoryId());
                List<Category> list = categoryService.list(wrapper1);
                category1.setCategories(list);
            }
        }
        resultVo.setMsg("ok");
        resultVo.setCode("10000");
        resultVo.setDate(categories);
        return resultVo;
    }

    @GetMapping("/getAll0")
    public List<Category> getAll0(Integer level){
        return categoryService.getAll0(level);
    }

}
